package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelRuleEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantThemeEntity;
import com.dragon.modules.yuegou.entity.merchant.ThemeEntity;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleIsDefault;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantThemeStatus;
import com.dragon.modules.yuegou.enums.merchant.ThemeIsDefault;
import com.dragon.modules.yuegou.enums.merchant.ThemeStatus;
import com.dragon.modules.yuegou.enums.merchant.ThemeType;
import com.dragon.modules.yuegou.mapper.merchant.MerchantThemeMapper;
import com.dragon.modules.yuegou.model.merchant.MerchantTheme;
import com.dragon.modules.yuegou.model.merchant.Theme;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelRuleQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantThemeQuery;
import com.dragon.modules.yuegou.query.merchant.ThemeQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelRuleService;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelService;
import com.dragon.modules.yuegou.service.merchant.MerchantThemeService;
import com.dragon.modules.yuegou.service.merchant.ThemeService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商家主题 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 15:32:57
 * @version 2.0
 */
@Service("merMerchantThemeService")
public class MerchantThemeServiceImpl
		extends ServiceImpl<MerchantThemeMapper, MerchantThemeEntity>
		implements MerchantThemeService {
	@Autowired
	private ThemeService themeService;
	
	@Autowired
	private MerchantLevelService merchantLevelService;
	
	@Autowired
	private MerchantLevelRuleService merchantLevelRuleService;
	/**
	 * 商户添加主题模板
	 * @param id
	 * @param merchantId
	 * @return
	 */
	@Transactional
	@Override
	public void addTheme(String id, String merchantId) {
		Check.notNull(id, "主题模板id不能为空！");
		Check.notNull(merchantId, "商户信息不能为空！");
		Theme theme = themeService.searchOne(id);
		//依据商户id得到等级id的集合
		List<String> levelIdList = merchantUseRule(merchantId);
		//如果是默认的模板直接添加不需要商户等级
		//判断商户是否等级足够添加该模板
		if (ThemeIsDefault.NO.equals(theme.getIsDefault())&&!levelIdList.contains(theme.getLevelId())) {
			throw new DragonException(10000,"等级不够");

		}

		//查询该商户是否已存在相同的商家主题
		MerchantThemeQuery merchantThemeQuery = new MerchantThemeQuery();
		merchantThemeQuery.setMerchantId(merchantId);
		merchantThemeQuery.setThemeId(id);
		MerchantThemeEntity merchantTheme = getOne(merchantThemeQuery.toQueryWrapper());
		//查询现有效的商家主题
		MerchantThemeQuery merchantThemeQuery1 = new MerchantThemeQuery();
		merchantThemeQuery1.setMerchantId(merchantId);
		merchantThemeQuery1.setStatus(MerchantThemeStatus.VALID);
		MerchantThemeEntity validTheme = getOne(merchantThemeQuery1.toQueryWrapper());

		//存在即修改状态即可
		if (Objects.nonNull(merchantTheme)) {
			if (MerchantThemeStatus.VALID.equals(merchantTheme.getStatus())) {
				return;
			}
			merchantTheme.setStatus(MerchantThemeStatus.VALID);
			updateById(merchantTheme);
			validTheme.setStatus(MerchantThemeStatus.INVALID);
			updateById(validTheme);

		}
		//不存在重新创建并修改之前的
		else {
			MerchantTheme merchantTheme1 = new MerchantTheme();
			merchantTheme1.setMerchantId(merchantId);
			merchantTheme1.setThemeId(id);
			merchantTheme1.setThemeName(theme.getName());
			merchantTheme1.setStatus(MerchantThemeStatus.VALID);
			save(merchantTheme1);
			if (Objects.nonNull(validTheme)) {
				validTheme.setStatus(MerchantThemeStatus.INVALID);
				updateById(validTheme);
			}

		}
	}

	/**
	 * 通过商户的id获取商户能够使用的等级的集合
	 * @param merchantId
	 * @return
	 */
	public List<String> merchantUseRule(String merchantId) {
		MerchantLevelQuery merchantLevelQuery = new MerchantLevelQuery();
		merchantLevelQuery.setMerchantId(merchantId);
		merchantLevelQuery.setStatus(MerchantLevelStatus.PAID);
		MerchantLevelEntity merchantLevel = merchantLevelService.getOne(merchantLevelQuery.toQueryWrapper());
		ArrayList<String> levelIdList = new ArrayList<>();
		if (Objects.nonNull(merchantLevel)) {
			levelIdList.add(merchantLevel.getLevelId());
			MerchantLevelRuleQuery ruleQuery = new MerchantLevelRuleQuery();
			ruleQuery.setStatus(MerchantLevelRuleStatus.NORMAL);
			ruleQuery.setLevelName("高级商家");
			MerchantLevelRuleEntity levelRule = merchantLevelRuleService.getOne(ruleQuery.toQueryWrapper());
			ruleQuery.setLevelName("旗舰商家");
			MerchantLevelRuleEntity one = merchantLevelRuleService.getOne(ruleQuery.toQueryWrapper());
			if (Objects.nonNull(levelRule)&&Objects.nonNull(one)&&Objects.equals(merchantLevel.getLevelId(), one.getId())) {
				levelIdList.add(levelRule.getId());
			}

		}
		//商家等级表中不包含基础商家的时候集合中添加
		MerchantLevelRuleQuery merchantLevelRuleQuery = new MerchantLevelRuleQuery();
		merchantLevelRuleQuery.setIsDefault(MerchantLevelRuleIsDefault.YES);
		merchantLevelRuleQuery.setStatus(MerchantLevelRuleStatus.NORMAL);
		MerchantLevelRuleEntity one = merchantLevelRuleService.getOne(merchantLevelRuleQuery.toQueryWrapper());
		if (Objects.nonNull(one) && (!CollectionUtils.isEmpty(levelIdList) || !levelIdList.contains(one.getId()))) {
			levelIdList.add(one.getId());
		}
		return levelIdList;
	}
	/**
	 * 通过商户id查询具体的模板
	 *
	 * @param merchantId
	 * @return
	 */
	@Override
	public Theme getThemeByMerchantId(String merchantId) {
		//商户主题表  通过商户的id、status查询出正在使用的模板id
		MerchantThemeQuery merchantThemeQuery = new MerchantThemeQuery();
		merchantThemeQuery.setMerchantId(merchantId);
		merchantThemeQuery.setStatus(MerchantThemeStatus.VALID);
		MerchantThemeEntity one = getOne(merchantThemeQuery.toQueryWrapper());
		if (Objects.isNull(one)) {
			ThemeQuery themeQuery = new ThemeQuery();
			themeQuery.setStatus(ThemeStatus.PUTAWAY);
			themeQuery.setIsDefault(ThemeIsDefault.YES);
			themeQuery.setType(ThemeType.COMMONTEMPLATE);
			return new Theme(themeService.getOne(themeQuery.toQueryWrapper()));
		}
		return themeService.searchOne(one.getThemeId());
	}

	@Override
	public void updateLevelFailureMerchantTheme() {
		//查询已经购买会员等级并且过了有效期的等级信息
		MerchantLevelQuery levelQuery = new MerchantLevelQuery();
		levelQuery.setStatus(MerchantLevelStatus.PAID);
		levelQuery.startTime().lt(LocalDateTime.now());
		levelQuery.endTime().gt(LocalDateTime.now());
		List<MerchantLevelEntity> levels = merchantLevelService.list(levelQuery.toQueryWrapper());
		
		//获取商户ID集合
		List<String> merchatIds = levels.stream().map(level -> level.getMerchantId()).collect(Collectors.toList());
		
		//查询默认主题
		ThemeQuery themeQuery = new ThemeQuery();
		themeQuery.setStatus(ThemeStatus.PUTAWAY);
		themeQuery.setIsDefault(ThemeIsDefault.YES);
		
		//查询基础会员等级默认皮肤
		MerchantLevelRuleQuery levelRuleQuery = new MerchantLevelRuleQuery();
		levelRuleQuery.setLevelName("基础商家");
		MerchantLevelRuleEntity merchantLevelRule = merchantLevelRuleService.getOne(levelRuleQuery.toQueryWrapper());
		//设置商户等级ID
		themeQuery.setLevelId(merchantLevelRule.getId());
		ThemeEntity theme = themeService.getOne(themeQuery.toQueryWrapper());
		
		//修改登记过期商家的主题皮肤
		MerchantTheme merchantTheme = new MerchantTheme();
		merchantTheme.setThemeId(theme.getId());
		merchantTheme.setThemeName(theme.getName());
		
		MerchantThemeQuery merchantThemeQuery = new MerchantThemeQuery();
		merchantThemeQuery.merchantId().in(merchatIds);
		this.update(merchantTheme, merchantThemeQuery.toQueryWrapper());
	}
}